#include<bits/stdc++.h>
using namespace std;

struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

// 检测是否是pair
template <typename T>
struct is_pair : std::false_type
{
};
template <typename T, typename U>
struct is_pair<std::pair<T, U> > : std::true_type
{
};
template <typename T>
inline constexpr bool is_pair_v = is_pair<T>::value;

// 检测输出函数是否存在
template <typename T>
struct has_output_function
{
    template <class U>
    static auto output(U *ptr)
        -> decltype(std::declval<std::ostream &>() << *ptr,
                    std::true_type());

    template <class U>
    static std::false_type output(...);

    static constexpr bool value =
        decltype(output<T>(nullptr))::value;
};

template <typename T>
inline constexpr bool has_output_function_v =
    has_output_function<T>::value;

enum CHARS
{
    ORD,   // 其他类型
    CHAR,  // char 类型
    STRING // string 类型
};

template <typename T>
int ischarOrString(T &elem)
{
    using std::decay_t;
    using std::is_same_v;
    using element_type = decay_t<decltype(elem)>;
    constexpr bool is_char_v = is_same_v<element_type, char>;
    constexpr bool is_string_v = is_same_v<element_type, char *> ||
                                 is_same_v<element_type, const char *> ||
                                 is_same_v<element_type, std::string>;
    if (is_char_v)
        return CHAR;
    else if (is_string_v)
        return STRING;
    else
        return ORD;
}

template <typename T>
void output(T &elem, int type, std::ostream &os)
{
    switch (type)
    {
    case CHAR:
        os << '\'' << elem << '\'';
        break;
    case STRING:
        os << '\"' << elem << '\"';
        break;
    case ORD:
        os << elem;
        break;
    }
}

template <typename T, typename Cont>
auto output_element(std::ostream &os, const T &element,
                    const Cont &)
    -> typename std::enable_if<is_pair<typename Cont::value_type>::value, bool>::type
{
    int ftype = ischarOrString(element.first);
    int stype = ischarOrString(element.second);

    output(element.first, ftype, os);
    os << " => ";
    output(element.second, stype, os);
    return true;
}

template <typename T, typename Cont>
auto output_element(std::ostream &os, const T &element,
                    const Cont &)
    -> typename std::enable_if<!is_pair<typename Cont::value_type>::value, bool>::type
{
    int etype = ischarOrString(element);
    output(element, etype, os);
    return false;
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &pr)
{
    os << '(' << pr.first << ", " << pr.second << ')';
    return os;
}

// 针对没有输出函数的容器处理
template <typename T,
          typename = std::enable_if_t<!has_output_function_v<T> > >
auto operator<<(std::ostream &os, const T &container)
    -> decltype(container.begin(), container.end(), os)
{
    os << "{ ";
    if (!container.empty())
    {
        bool on_first_element = true;
        for (auto elem : container)
        {
            if (!on_first_element)
            {
                os << ", ";
            }
            else
            {
                on_first_element = false;
            }
            output_element(os, elem, container);
        }
    }
    os << " }";
    return os;
}


void trimLeftTrailingSpaces(string &input) {
    input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
        return !isspace(ch);
    }));
}

void trimRightTrailingSpaces(string &input) {
    input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
        return !isspace(ch);
    }).base(), input.end());
}

TreeNode* stringToTreeNode(string input) {
    trimLeftTrailingSpaces(input);
    trimRightTrailingSpaces(input);
    input = input.substr(1, input.length() - 2);
    if (!input.size()) {
        return nullptr;
    }

    string item;
    stringstream ss;
    ss.str(input);

    getline(ss, item, ',');
    TreeNode* root = new TreeNode(stoi(item));
    queue<TreeNode*> nodeQueue;
    nodeQueue.push(root);

    while (true) {
        TreeNode* node = nodeQueue.front();
        nodeQueue.pop();

        if (!getline(ss, item, ',')) {
            break;
        }

        trimLeftTrailingSpaces(item);
        if (item != "null") {
            int leftNumber = stoi(item);
            node->left = new TreeNode(leftNumber);
            nodeQueue.push(node->left);
        }

        if (!getline(ss, item, ',')) {
            break;
        }

        trimLeftTrailingSpaces(item);
        if (item != "null") {
            int rightNumber = stoi(item);
            node->right = new TreeNode(rightNumber);
            nodeQueue.push(node->right);
        }
    }
    return root;
}




class Solution {
public:
    int minv = INT_MAX, fminv = INT_MAX;
    
    int findSecondMinimumValue(TreeNode* root) {
        
        queue<TreeNode* > q;
        
        q.push(root);
        minv = min(minv, root->val);
        fminv = min(fminv, root->val);
        
        while (q.size()) {
            TreeNode * t = q.front();q.pop();
            if (t->left != nullptr) q.push(t->left);
            if (t->right != nullptr) q.push(t->right);
            
            if (t->val < minv) fminv = minv, minv = t->val;
            else if (t->val < fminv && t->val != minv) fminv = t->val;
            else if (minv == fminv) fminv = t->val;
  
        }
        // cout << fminv << " " << minv << endl;
        return fminv == minv ? -1 : fminv;
        
    }
};


int main() {
    string line;
    while (getline(cin, line)) {
        TreeNode* root = stringToTreeNode(line);
        
        int ret = Solution().findSecondMinimumValue(root);

        string out = to_string(ret);
        cout << out << endl;
    }
    return 0;
}